<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>个人待办系统</title>
    <style>
        :root {
            --bg-color: #f8f9fa;
            --card-bg-color: #ffffff;
            --text-color: #212529;
            --muted-text-color: #6c757d;
            --border-color: #dee2e6;
            --primary-color: #6f42c1;
            --primary-text-color: #ffffff;
            --shadow-color: rgba(0, 0, 0, 0.05);
            --completed-bg-color: #f1f3f5;
            --completed-text-color: #adb5bd;
        }

        [data-theme="dark"] {
            --bg-color: #121212;
            --card-bg-color: #1e1e1e;
            --text-color: #e9ecef;
            --muted-text-color: #adb5bd;
            --border-color: #495057;
            --primary-color: #8a63d2;
            --primary-text-color: #ffffff;
            --shadow-color: rgba(0, 0, 0, 0.2);
            --completed-bg-color: #2c2c2c;
            --completed-text-color: #6c757d;
        }

        body {
            font-family: "Microsoft YaHei",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
            max-width: 1000px;
            margin: 40px auto;
            padding: 20px;
            background-color: var(--bg-color);
            color: var(--text-color);
            transition: background-color 0.3s, color 0.3s;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }
        .header h1 a {
            font-weight: 600;
            color: var(--primary-color);
        }
        .nav {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .nav a {
            color: var(--muted-text-color);
            text-decoration: none;
            font-weight: 500;
            transition: color 0.2s;
        }
        .nav a:hover {
            color: var(--primary-color);
        }
        #theme-toggle {
            background: none;
            border: 1px solid var(--border-color);
            color: var(--muted-text-color);
            padding: 5px 10px;
            border-radius: 5px;
            cursor: pointer;
        }
        .todo-item {
            background-color: var(--card-bg-color);
            border: 1px solid var(--border-color);
            padding: 20px;
            margin: 15px 0;
            border-radius: 8px;
            position: relative;
            box-shadow: 0 4px 6px var(--shadow-color);
            transition: all 0.2s ease-in-out;
        }
        .todo-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px var(--shadow-color);
        }
        .priority-high { border-left: 5px solid #dc3545; }
        .priority-medium { border-left: 5px solid #ffc107; }
        .priority-low { border-left: 5px solid #28a745; }
        .completed {
            background-color: var(--completed-bg-color);
            color: var(--completed-text-color);
            text-decoration: line-through;
            opacity: 0.8;
        }
        .due-date {
            font-size: 0.9em;
            color: var(--muted-text-color);
            margin: 5px 0 10px;
        }
        .priority-badge {
            position: absolute;
            top: 15px;
            right: 15px;
            padding: 3px 8px;
            border-radius: 5px;
            font-size: 0.8em;
            color: white;
            font-weight: bold;
        }
        .priority-high .priority-badge { background-color: #dc3545; }
        .priority-medium .priority-badge { background-color: #ffc107; color: #212529; }
        .priority-low .priority-badge { background-color: #28a745; }
        .category-tag {
            display: inline-block;
            background-color: var(--bg-color);
            border: 1px solid var(--border-color);
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 0.8em;
            margin-right: 5px;
        }
        .stats-card {
            border: 1px solid #ddd;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .search-form {
            display: flex;
            margin-bottom: 20px;
        }
        .search-form input {
            flex: 1;
            margin-right: 10px;
        }
    </style>
    <!-- FullCalendar CSS -->
    <link href="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.css" rel="stylesheet">
</head>
<body>
    <div class="header">
        <h1><a href="{% url 'index' %}" style="text-decoration: none;">个人待办事项管理</a></h1>
        <div class="nav">
            <button id="theme-toggle">切换模式</button>
            {% if user.is_authenticated %}
                <span>欢迎, {{ user.username }}!</span>
                <a href="{% url 'add_todo' %}">新增</a>
                <a href="{% url 'manage_categories' %}">分类管理</a>
                <a href="{% url 'task_report' %}">任务报表</a>
                <a href="{% url 'manage_templates' %}">模板管理</a>
                <a href="{% url 'calendar_view' %}">日历视图</a>
                <form action="{% url 'logout' %}" method="post" style="display: inline;">
                    {% csrf_token %}
                    <button type="submit" style="background:none; border:none; color:var(--muted-text-color); font-size: inherit; cursor:pointer; padding:0; font-weight:500;">退出登录</button>
                </form>
            {% else %}
                <a href="{% url 'login' %}">登录</a>
                <a href="{% url 'register' %}">注册</a>
            {% endif %}
        </div>
    </div>
    <hr>

    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }}" role="alert">
                {{ message }}
            </div>
        {% endfor %}
    {% endif %}

    <div class="search-form">
        <form action="{% url 'search_todos' %}" method="get">
            <input type="text" name="q" placeholder="搜索任务...">
            <button type="submit">搜索</button>
        </form>
    </div>

    {% block content %}{% endblock %}

    <!-- FullCalendar JS -->
    <script src="https://cdn.jsdelivr.net/npm/fullcalendar@5.11.3/main.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const themeToggle = document.getElementById('theme-toggle');
            const htmlEl = document.documentElement;
            const currentTheme = localStorage.getItem('theme');

            // 1. 优先使用本地存储的用户选择
            if (currentTheme) {
                htmlEl.setAttribute('data-theme', currentTheme);
            } else {
                // 2. 如果没有用户选择，则根据时间判断
                const hour = new Date().getHours();
                if (hour >= 22 || hour < 6) {
                    htmlEl.setAttribute('data-theme', 'dark');
                }
            }

            // 切换按钮的点击事件
            themeToggle.addEventListener('click', () => {
                const currentTheme = htmlEl.getAttribute('data-theme');
                const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
                htmlEl.setAttribute('data-theme', newTheme);
                // 将用户的选择保存到本地存储
                localStorage.setItem('theme', newTheme);
            });
        });
    </script>
</body>
</html>